package com.arthur.shopping.controller;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.arthur.common.annotation.Log;
import com.arthur.common.controller.BaseController;
import com.arthur.common.domain.ResponseBo;
import com.arthur.common.domain.Tree;
import com.arthur.common.util.FileUtils;
import com.arthur.shopping.domain.Type;
import com.arthur.shopping.service.TypeService;

@Controller
public class TypeController extends BaseController {
	@Autowired
	private TypeService typeService;

	@RequestMapping("shopping/type")
	public String index() {
		return "shopping/type/list";
	}

	@RequestMapping("shopping/type/getType")
	@ResponseBody
	public ResponseBo getType(Long typeId) {
		try {
			Type type = this.typeService.findById(typeId);
			return ResponseBo.ok(type);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("获取信息失败，请联系网站管理员！");
		}
	}
	
	@RequestMapping("shopping/type/typeNameTree")
	@ResponseBody
	public ResponseBo getTypeButtonTree() {
		try {
			Tree<Type> tree = this.typeService.getTypeNameTree();
			return ResponseBo.ok(tree);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("获取类型列表失败！");
		}
	}
	
	@RequestMapping("shopping/type/tree")
	@ResponseBody
	public ResponseBo getTypeTree() {
		try {
			Tree<Type> tree = this.typeService.getTypeTree();
			return ResponseBo.ok(tree);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("获取类型列表失败！");
		}
	}
	
	@RequestMapping("shopping/type/list")
	@ResponseBody
	public List<Type> typeList(Type type) {
		try {
			return this.typeService.findAllTypes(type);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@RequestMapping("shopping/type/excel")
	@ResponseBody
	public ResponseBo typeExcel(Type type) {
		try {
			List<Type> list = this.typeService.findAllTypes(type);
			return FileUtils.createExcelByPOIKit("类型表", list, Type.class);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("导出Excel失败，请联系网站管理员！");
		}
	}

	@RequestMapping("shopping/type/csv")
	@ResponseBody
	public ResponseBo typeCsv(Type type){
		try {
			List<Type> list = this.typeService.findAllTypes(type);
			return FileUtils.createCsv("类型表", list, Type.class);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("导出Csv失败，请联系网站管理员！");
		}
	}
	
	@RequestMapping("shopping/type/checkTypeName")
	@ResponseBody
	public boolean checkTypeName(String typeName, String oldTypeName) {
		if (StringUtils.isNotBlank(oldTypeName) && typeName.equalsIgnoreCase(oldTypeName)) {
			return true;
		}
		Type result = this.typeService.findByName(typeName);
		if (result != null)
			return false;
		return true;
	}

	@Log("新增类型/按钮")
	@RequiresPermissions("type:add")
	@RequestMapping("shopping/type/add")
	@ResponseBody
	public ResponseBo addType(Type type) {
		try {
			this.typeService.addType(type);
			return ResponseBo.ok("新增类型成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("新增类型失败，请联系网站管理员！");
		}
	}

	@Log("删除类型")
	@RequiresPermissions("type:delete")
	@RequestMapping("shopping/type/delete")
	@ResponseBody
	public ResponseBo deleteTypes(String ids) {
		try {
			this.typeService.deleteMeuns(ids);
			return ResponseBo.ok("删除成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("删除失败，请联系网站管理员！");
		}
	}
	
	@Log("修改类型")
	@RequiresPermissions("type:update")
	@RequestMapping("shopping/type/update")
	@ResponseBody
	public ResponseBo updateType(Type type) {
		try {
			this.typeService.updateType(type);
			return ResponseBo.ok("修改类型成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("修改类型失败，请联系网站管理员！");
		}
	}
}
